package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.vo.SaveSpuVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/product")
@RestController
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    //    http://api.gmall.com/admin/product/1/10?category3Id=61
// 分页 当前页码和每页大小
    @GetMapping("/{pn}/{ps}")
    public Result spuList(@PathVariable Long pn,
                          @PathVariable Long ps,
                          @RequestParam Long category3Id) {

        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SpuInfo> eq = wrapper.lambda()
                .eq(SpuInfo::getCategory3Id, category3Id);
        Page<SpuInfo> page = new Page<>(pn, ps);
        Page<SpuInfo> spuInfoPage = spuInfoService.page(page, eq);

        return Result.ok(spuInfoPage);
    }

    //    保存
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SaveSpuVo vo) {
        spuInfoService.saveSpuInfo(vo);
        return Result.ok();
    }

//    获取spu的图片
@GetMapping("/spuImageList/{spuId}")
public Result spuImageList(@PathVariable("spuId") Long spuId){
    List<SpuImage> list = spuImageService.lambdaQuery()
            .eq(SpuImage::getSpuId, spuId)
            .list();

    return Result.ok(list);
    }
}
